package com.cloud.quickcore;

import com.cloud.quickcommon.utils.RoleUtile;
import com.cloud.quickcore.admin.dto.SysRoleDto;
import com.cloud.quickcore.admin.entity.SysRole;
import com.cloud.quickcore.admin.entity.SysUser;
import com.cloud.quickcore.admin.service.SysRoleService;
import com.cloud.quickcore.admin.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author NiJo_H
 * @version 1.0
 */
@Slf4j
@Component
public class InitProject implements ApplicationRunner {
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysUserService userService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        SysRole roleCode = roleService.findRoleCode(RoleUtile.SUPER_ADMIN_CODE);
        if(roleCode ==null){
            log.info("检测数据库角色表未超级管理员即将创建...");
            SysRoleDto dto=new SysRoleDto();
            dto.setRoleName("超级管理员");
            dto.setRoleCode(RoleUtile.SUPER_ADMIN_CODE);
            roleService.save(dto);
        }
        if(userService.findSysUserByUsername("root")==null){
            log.info("检测数据库root用户不存在即将创建...");
            SysUser sysUser=new SysUser();
            sysUser.setUsername("root");
            sysUser.setPassword("root");
            sysUser.setEmail("root@163.com");
            sysUser.setName("超级管理员");
            userService.saveSuperAdmin(sysUser);
        }
    }
}
